package com.cibfintech.rpa.jdwzcx.service;

import java.io.ByteArrayOutputStream;
import java.util.concurrent.Future;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.mail.internet.MimeMessage;

import org.apache.poi.ooxml.POIXMLProperties.CoreProperties;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.cibfintech.rpa.jdwzcx.common.dto.SearchResult;

@Service
public class JdwzcxService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private JdwzcxWebRunner webRunner;

    @Value("${spring.mail.username}")
    private String from;

    @Async
    public void investigate(String receiver, String name, String cardNumber, String type) throws Exception {
        Future<SearchResult> shixinCsrcFuture = webRunner.shixinCsrc(name);
        Future<SearchResult> hdChinataxFuture = webRunner.hdChinatax(name, type);
        Future<SearchResult> zhixingCourtFuture = webRunner.zhixingCourt(name, cardNumber);
        Future<SearchResult> websearchMeeFuture = webRunner.websearchMee(name);

        SearchResult shixinCsrcResult = shixinCsrcFuture.get();
        SearchResult hdChinataxResult = hdChinataxFuture.get();
        SearchResult courtZhixingResult = zhixingCourtFuture.get();
        SearchResult websearchMeeResult = websearchMeeFuture.get();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);
        ZipEntry zipEntry;
        zipEntry = new ZipEntry("证券期货市场失信记录查询平台.png");
        zipOutputStream.putNextEntry(zipEntry);
        zipOutputStream.write(shixinCsrcResult.screenshotBytes());

        zipEntry = new ZipEntry("重大税收违法案件信息公布栏.png");
        zipOutputStream.putNextEntry(zipEntry);
        zipOutputStream.write(hdChinataxResult.screenshotBytes());

        courtZhixingResult = zhixingCourtFuture.get();
        zipEntry = new ZipEntry("中国执行信息公开网.png");
        zipOutputStream.putNextEntry(zipEntry);
        zipOutputStream.write(courtZhixingResult.screenshotBytes());

        zipEntry = new ZipEntry("环境保护部检索.png");
        zipOutputStream.putNextEntry(zipEntry);
        zipOutputStream.write(websearchMeeResult.screenshotBytes());

        XSSFWorkbook workbook = new XSSFWorkbook();
        CoreProperties properties = workbook.getProperties().getCoreProperties();
        properties.setCreator("兴业数金");
        Sheet sheet = workbook.createSheet("Sheet1");
        Row row = sheet.createRow(0);
        row.createCell(2).setCellValue("证券期货市场失信记录查询平台");
        row.createCell(3).setCellValue("重大税收违法案件信息公布栏");
        row.createCell(4).setCellValue("中国执行信息公开网");

        row = sheet.createRow(1);
        row.createCell(0).setCellValue(name);
        row.createCell(1).setCellValue(cardNumber);
        row.createCell(2).setCellValue(shixinCsrcResult.message());
        row.createCell(3).setCellValue(hdChinataxResult.message());
        row.createCell(4).setCellValue(courtZhixingResult.message());

        zipEntry = new ZipEntry("查询结果.xlsx");
        zipOutputStream.putNextEntry(zipEntry);
        workbook.write(zipOutputStream);
        workbook.close();

        zipOutputStream.close();
        byte[] byteArray = out.toByteArray();

        String text = String.format("关于%s的信息已查询完成，请查收附件", name);
//		MimeMessage mimeMessage = mailSender.createMimeMessage();
//		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
//		messageHelper.setFrom(from);
//		messageHelper.setTo(receiver);
//		messageHelper.setSubject("尽调网站查询结果");
//		messageHelper.setText(text);
//
//		ByteArrayResource byteArrayResource = new ByteArrayResource(byteArray);
//		messageHelper.addAttachment("result.zip", byteArrayResource);
//
//		mailSender.send(mimeMessage);
    }

}
